package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementTemplate;
import com.xcity.game.common.ItemResponse;
import com.xcity.game.item.ItemInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class AchievementListResponse extends Response {
	
	public static class AchievementInfo implements Comparable<AchievementInfo> {
		private int id; // !!! 这id是成就类型，不是成就id !!!
		private String achievesName;
		private String desc;
		private String img;
		private int gold;
		private int stone;
		private List<ItemResponse> items; // 奖励物品
//		private int completeValue; // 达成目标需求值
		private boolean isHas; // 是否达成目标
		private boolean isReceived; // 是否领取奖励
//		private String atype;
		public AchievementInfo() {}
		public AchievementInfo(Achievement ref) {
			AchievementTemplate template = ref.getTemplate();
			this.id = template.getGroup().ordinal(); // 成就类型
			this.achievesName = template.getName();
			this.desc = template.getDesc();
			this.img = template.getIcon();
			this.gold = template.getGold();
			this.stone = template.getStone();
			if (template.getRewardItems() != null) {
				ItemInfo[] refItems = template.getRewardItems();
				items = new ArrayList<ItemResponse>(refItems.length);
				for (ItemInfo refItem : refItems) {
					items.add(new ItemResponse(refItem));
				}
			} else {
				items = Collections.emptyList();
			}
			this.isHas = ref.getState() != State.INITED;
			this.isReceived = ref.getState() == State.REWARDED;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getAchievesName() {
			return achievesName;
		}
		public void setAchievesName(String achievesName) {
			this.achievesName = achievesName;
		}
		public String getDesc() {
			return desc;
		}
		public void setDesc(String desc) {
			this.desc = desc;
		}
		public String getImg() {
			return img;
		}
		public void setImg(String img) {
			this.img = img;
		}
		public int getGold() {
			return gold;
		}
		public void setGold(int gold) {
			this.gold = gold;
		}
		public int getStone() {
			return stone;
		}
		public void setStone(int stone) {
			this.stone = stone;
		}
//		public int getCompleteValue() {
//			return completeValue;
//		}
//		public void setCompleteValue(int completeValue) {
//			this.completeValue = completeValue;
//		}
		public List<ItemResponse> getItems() {
			return items;
		}
		public void setItems(List<ItemResponse> items) {
			this.items = items;
		}
		public boolean isIsHas() {
			return isHas;
		}
		public void setIsHas(boolean isHas) {
			this.isHas = isHas;
		}
		public boolean isIsReceived() {
			return isReceived;
		}
		public void setIsReceived(boolean isReceived) {
			this.isReceived = isReceived;
		}
		@Override
		public int compareTo(AchievementInfo o) {
			// 权重越低越排前,权重相同则id越小越排前
			int w1 = weight();
			int w2 = o.weight();
			if (w1 == w2) {
				return id - o.id;
			}
			return w1 - w2;
		}
		private int weight() {
			if (isHas && !isReceived) { // 已完成未领取
				return 0;
			}
			if (!isHas) { // 未完成
				return 1;
			}
			return 2; // 已完成已领取
		}
	}
	
	private List<AchievementInfo> achieves;
	
	public AchievementListResponse(int serial, List<Achievement> srcs) {
		super(serial);
		int size;
		if (srcs == null || (size = srcs.size()) == 0) {
			achieves = Collections.emptyList();
			return;
		}
		achieves = new ArrayList<AchievementInfo>(size);
		for (int i = 0; i < size; i++) {
			Achievement src = srcs.get(i);
			AchievementInfo info = new AchievementInfo(src);
			achieves.add(info);
		}
		Collections.sort(achieves);
	}

	public List<AchievementInfo> getAchieves() {
		return achieves;
	}

	public void setAchieves(List<AchievementInfo> achieves) {
		this.achieves = achieves;
	}
}
